package com.dev;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Environment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.fb.FileBrowserPage;
import java.io.File;
public class DefaultPage extends LinearLayout implements OnClickListener{
	MainActivity ma;
	TransverseItem tiStorage;
	TransverseItem tiAudio,tiVideo,tiImage;
	TransverseItem tiApps;
	TransverseItem tiTrash;
	TransverseItem tiWorkDir;
	TransverseItem tiInternal;
	TransverseItem tiDownloads;
	TransverseItem tiCamera;
	TransverseItem tiScreenShots;
	TextView title;
    DefaultPage(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		tiStorage=new TransverseItem(ma);
		tiStorage.setOnClickListener(this);
		tiStorage.setText("内置SD卡");
		tiAudio=new TransverseItem(ma);
		tiAudio.setOnClickListener(this);
		tiAudio.setText("多媒体音乐库");
		tiImage=new TransverseItem(ma);
		tiImage.setOnClickListener(this);
		tiImage.setText("多媒体图片库");
		tiApps=new TransverseItem(ma);
		tiApps.setOnClickListener(this);
		tiApps.setText("应用管理");
		tiTrash=new TransverseItem(ma);
		tiTrash.setOnClickListener(this);
		tiTrash.setText("回收站");
		tiInternal=new TransverseItem(ma);
		tiInternal.setOnClickListener(this);
		tiInternal.setText("内部目录");
		tiWorkDir=new TransverseItem(ma);
		tiWorkDir.setOnClickListener(this);
		tiWorkDir.setText("输出目录");
		tiDownloads=new TransverseItem(ma);
		tiDownloads.setOnClickListener(this);
		tiDownloads.setText("Downloads");
		tiScreenShots=new TransverseItem(ma);
		tiScreenShots.setOnClickListener(this);
		tiScreenShots.setText("ScreenShots");
		title=new TextView(ma);
		title.setText("开始");
		title.setTextColor(0xff7f7f7f);
		title.setGravity(Gravity.CENTER);
		title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
		title.setTypeface(Typeface.DEFAULT,Typeface.BOLD_ITALIC);
		addView(title,-1,(int)(33*ma.dp));
		addView(tiStorage,-1,-2);
		addView(tiAudio,-1,-2);
		addView(tiImage,-1,-2);
		addView(tiTrash,-1,-2);
		addView(tiWorkDir,-1,-2);
		addView(tiInternal,-1,-2);
		addView(tiApps,-1,-2);
		addView(tiDownloads,-1,-2);
		addView(tiScreenShots,-1,-2);
	}
	@Override
	public void onClick(View v) {
		if(v==tiStorage){//onClick
			if(Environment.isExternalStorageManager()){
				File ext=Environment.getExternalStorageDirectory();
                FileBrowserTab fbt=new FileBrowserTab(ma);
				TabData.FileBrowserInfo fbi=new TabData.FileBrowserInfo();
				fbi.home=ext;
				fbi.currentDir=ext;
				fbt.fbi=fbi;
				fbt.setOnClickListener(ma.gui.TabOnClick);
				fbt.setText(fbi.home.getName());
				ma.gui.tabs.addView(fbt,TabBar.tablp);
				ma.gui.TabOnClick.onClick(fbt);
				ma.tabList.addTabInfo(fbt.fbi);
				ma.tdm.updateSavedTabList(ma.tabList);
			}else{
				Toast.makeText(ma,"请赋予访问权限",0).show();
			}
		}else if(v==tiAudio){//onClick
            
			for(int index=0,count=ma.gui.tabs.getChildCount();true;index++){
				if(index<count){
					View child=ma.gui.tabs.getChildAt(index);
					if(child instanceof FileBrowserTab){
						FileBrowserTab fbt=(FileBrowserTab)child;
						if(fbt.der==10){
							ma.gui.hsv.setScrollX(fbt.getLeft());
							ma.gui.TabOnClick.onClick(fbt);
							break;
						}
					}
				}else{
					FileBrowserTab fbt=new FileBrowserTab(ma);
					fbt.setOnClickListener(ma.gui.TabOnClick);
					fbt.setText(TabData.MediaAudioBrowser.tabTitle);
					fbt.der=10;
					fbt.textUnderline=true;
					ma.gui.tabs.addView(fbt,TabBar.tablp);
					ma.gui.TabOnClick.onClick(fbt);
					ma.tabList.addTabInfo(new TabData.MediaAudioBrowser());
					ma.tdm.updateSavedTabList(ma.tabList);
					break;
				}
			}
		}else if(v==tiImage){//onClick
			for(int index=0,count=ma.gui.tabs.getChildCount();true;index++){
				if(index<count){
					View child=ma.gui.tabs.getChildAt(index);
					if(child instanceof FileBrowserTab){
						FileBrowserTab fbt=(FileBrowserTab)child;
						if(fbt.fbi==null&&fbt.der==20){
							ma.gui.hsv.setScrollX(fbt.getLeft());
							ma.gui.TabOnClick.onClick(fbt);
							break;
						}
					}
				}else{
					FileBrowserTab fbt=new FileBrowserTab(ma);
					fbt.setOnClickListener(ma.gui.TabOnClick);
					fbt.setText(TabData.MediaImageInfo.tabTitle);
					fbt.textUnderline=true;
					fbt.der=20;
					ma.gui.tabs.addView(fbt,TabBar.tablp);
					ma.gui.TabOnClick.onClick(fbt);
					ma.tabList.addTabInfo(new TabData.MediaImageInfo());
					ma.tdm.updateSavedTabList(ma.tabList);
					break;
				}
			}
		}else if(v==tiWorkDir){//onClick
			if(Environment.isExternalStorageManager()){
				File wk=ma.workDir;
				TabData.FileBrowserInfo fbi=new TabData.FileBrowserInfo();
				fbi.home=wk;
				fbi.currentDir=wk;
				FileBrowserTab fbt=new FileBrowserTab(ma);
				fbt.fbi=fbi;
				fbt.setOnClickListener(ma.gui.TabOnClick);
				fbt.setText(fbi.home.getName());
				ma.gui.tabs.addView(fbt,TabBar.tablp);
				ma.gui.TabOnClick.onClick(fbt);
				ma.tabList.addTabInfo(fbt.fbi);
				ma.tdm.updateSavedTabList(ma.tabList);
			}else{
				Toast.makeText(ma,"没有权限",0).show();
			}
		}else if(v==tiInternal){//onClick
			File ind=ma.getFilesDir();
			TabData.FileBrowserInfo fbi=new TabData.FileBrowserInfo();
			fbi.home=ind;
			fbi.currentDir=ind;
			FileBrowserTab fbt=new FileBrowserTab(ma);
			fbt.fbi=fbi;
			fbt.setOnClickListener(ma.gui.TabOnClick);
			fbt.setText(ind.getName());
			ma.gui.tabs.addView(fbt,TabBar.tablp);
			ma.gui.TabOnClick.onClick(fbt);
			ma.tabList.addTabInfo(fbt.fbi);
			ma.tdm.updateSavedTabList(ma.tabList);
		}else if(v==tiApps){//onClick
			for(int index=0,count=ma.gui.tabs.getChildCount();true;index++){
				if(index<count){
					View child=ma.gui.tabs.getChildAt(index);
					if(child instanceof AppListTab){
						ma.gui.TabOnClick.onClick(child);
						break;
					}
				}else{
					AppListTab alt=new AppListTab(ma);
					alt.setOnClickListener(ma.gui.TabOnClick);
					ma.gui.tabs.addView(alt,TabBar.tablp);
					ma.gui.TabOnClick.onClick(alt);
					TabData.AppListInfo ali=new TabData.AppListInfo();
					ma.tabList.addTabInfo(ali);
					ma.tdm.updateSavedTabList(ma.tabList);
					break;
				}
			}
		}else if(Environment.isExternalStorageManager()){//onClick
			if(v==tiDownloads){
				File downloads=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
				LinearLayout tabs=ma.gui.tabs;
				for(int index=0,count=tabs.getChildCount();true;index++){
					if(index<count){
						View child=tabs.getChildAt(index);
						if(child instanceof FileBrowserTab){
							FileBrowserTab fbt=(FileBrowserTab)child;
							if(fbt.fbi!=null){
								if(fbt.fbi.home.equals(downloads)){
									ma.gui.TabOnClick.onClick(fbt);
								}else{
									FileBrowserPage fbp=fbt.fileBrowserPage;
									if(fbp!=null){
										if(downloads.equals(fbp.currentDirectory)){
											ma.gui.TabOnClick.onClick(fbt);
										}
									}else if(downloads.equals(fbt.fbi.currentDir)){
										ma.gui.TabOnClick.onClick(fbt);
									}
								}
							}
						}
					}else{
						FileBrowserTab fbt=new FileBrowserTab(ma);
						TabData.FileBrowserInfo fbi=new TabData.FileBrowserInfo();
						fbi.home=downloads;
						fbi.currentDir=downloads;
						fbt.fbi=fbi;
						fbt.setText(downloads.getName());
						fbt.setOnClickListener(ma.gui.TabOnClick);
						tabs.addView(fbt,TabBar.tablp);
						ma.gui.TabOnClick.onClick(fbt);
						ma.gui.saveAllTab();
						break;
					}
				}
			}else if(v==tiScreenShots){
				File shots=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_SCREENSHOTS);
				LinearLayout tabs=ma.gui.tabs;
				for(int index=0,count=tabs.getChildCount();true;index++){
					if(index<count){
						View child=tabs.getChildAt(index);
						if(child instanceof FileBrowserTab){
							FileBrowserTab fbt=(FileBrowserTab)child;
							if(fbt.fbi!=null){
								if(shots.equals(fbt.fbi.home)){
									ma.gui.TabOnClick.onClick(fbt);
								}else{
									FileBrowserPage fbp=fbt.fileBrowserPage;
									if(fbp==null){
										if(shots.equals(fbt.fbi.currentDir)){
											ma.gui.TabOnClick.onClick(fbt);
										}
									}else if(shots.equals(fbp.currentDirectory)){
										ma.gui.TabOnClick.onClick(fbt);
									}
								}
							}
						}
					}else{
						FileBrowserTab fbt=new FileBrowserTab(ma);
						TabData.FileBrowserInfo fbi=new TabData.FileBrowserInfo();
						fbi.home=shots;
						fbi.currentDir=shots;
						fbt.fbi=fbi;
						fbt.setText(shots.getName());
						fbt.setOnClickListener(ma.gui.TabOnClick);
						tabs.addView(fbt,TabBar.tablp);
						ma.gui.TabOnClick.onClick(fbt);
						ma.gui.saveAllTab();
						break;
					}
				}
			}
		}else{
			Toast.makeText(ma,"无访问权限",0).show();
		}
	}
	public static class TransverseItem extends TextView{
		public TransverseItem(MainActivity ma){
			super(ma);
			setBackground(new GUI.Pressed());
			setGravity(Gravity.CENTER_VERTICAL);
			setTextColor(0xff000000);
			setTypeface(Typeface.MONOSPACE);
			setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
			setPadding(10,9,0,9);
		}
		@Override
		protected void onDraw(Canvas canvas){
			super.onDraw(canvas);
			Paint p=new Paint();
			p.setColor(0xff777777);
			int vw=getWidth();
			int vh=getHeight();
			canvas.drawRect(0,vh-1,vw,vh,p);
		}
	}
}
